package ds.tree
{
	public class TreeNode
	{
		public function TreeNode(parentNode:TreeNode = null,childNum:int = -1)
		{
			this.parentNode = parentNode;
			this._childNodes = new Vector.<TreeNode>();
			this._childNumLimit = childNum;
		}
		
		protected var _parentNode:TreeNode;
		protected var _childNodes:Vector.<TreeNode>;
		protected var _childNumLimit:uint;
		protected var _data:*;
		
		public function get data():*
		{
			return _data;
		}
		
		public function set data(data:*):void
		{
			this._data = data;
		}
		
		public function get childNodes():Vector.<TreeNode>
		{
			return _childNodes;
		}
		
		/**
		 *0:表示相等
		 * 1：表示大于
		 * -1：表示小于 
		 * @param data
		 * @return 
		 * 
		 */		
		public function compare(data:*):int
		{
			return 0;	
		}
		
		public function get parentNode():TreeNode
		{
			return _parentNode;
		}
		
		public function set parentNode(node:TreeNode ):void
		{
			this._parentNode = node;
		}
		
		/**
		 *是否是兄弟节点(具有相同父节点的节点互称为兄弟节点)
		 * @param node
		 * @return 
		 * 
		 */		
		public function isBrotherNode(node:TreeNode):Boolean
		{
			return this.parentNode && node.parentNode && parentNode == node.parentNode;
		}
		
		/**
		 *是否是堂兄弟节点（双亲在同一层的节点互为堂兄弟)
		 * @param node
		 * @return 
		 * 
		 */		
		public function isCousinNode(node:TreeNode):Boolean
		{
			if(!this.parentNode || !parentNode.parentNode ||!node.parentNode||!node.parentNode.parentNode)
				return false;
			
			return this.parentNode.parentNode == node.parentNode.parentNode;
		}
		
		public function disconnect(node:TreeNode):void
		{
			if( !node )
				return;
			
			var parentNode:TreeNode;
			var childNode:TreeNode;
			
			if( node.parentNode == this )
			{
				parentNode = this;
				childNode = node;
			}
			else if( this.parentNode == node )
			{
				parentNode = node;
				childNode = this;
			}
			
			childNode.parentNode = null;
			
			var childNodes:Vector.<TreeNode> = parentNode.childNodes;
			
			if(!childNodes)
				return;
			
			for( var i:uint = 0;i<childNodes.length;++i)
			{
				if(childNodes[i] == childNode)
				{
					childNodes.splice(i,1);
					break;
				}
			}
		}
		
		/**
		 *是否是叶节点 
		 * @return 
		 * 
		 */		
		public function get isLeaf():Boolean
		{
			if(_childNodes.fixed)
			{
				for(var i:uint = 0;i<_childNodes.length;++i)
				{
					if(_childNodes[i])
						return true;
				}
			}
			else
				return _childNodes.length > 0;
			
			return false;
		}
		
		/**
		 *获取节点深度 
		 * @return 
		 * 
		 */		
		public function get nodeDepth():uint
		{
			var depth:uint = 0;
			var parentNode:TreeNode = this.parentNode;
			while(parentNode)
			{
				depth++;
				parentNode = parentNode.parentNode;
			}
			
			return depth;
		}
		
		/**
		 *获取节点的高度（节点到一片树叶的最长路径的长）
		 * @return 
		 * 
		 */		
		public function get nodeHeight():uint
		{
			return 0;	
		}
	}
}